import 'package:dio/io.dart';
import 'package:flutter/material.dart';
import 'package:my_app/base/foundation/flutter_extension.dart';
import 'package:my_app/base/foundation/jd_cache.dart';
import 'package:my_app/base/net/jd_request.dart';
import 'package:my_app/base/router/router_util.dart';
import 'package:my_app/base/utils/jd_log.dart';
import 'package:my_app/base/widget/basic/jd_state.dart';
import 'package:my_app/modules/router/router_contract_page.dart';
import 'package:my_app/modules/widgets/jd_button.dart';
import 'package:my_app/modules/widgets/jd_scroll_view.dart';
import 'package:my_app/modules/widgets/jd_text_field.dart';

class DevProxyPage extends StatefulWidget {
  const DevProxyPage({
    super.key,
  });

  @override
  State<DevProxyPage> createState() => _DevProxyPageState();
}

class _DevProxyPageState extends JDState<DevProxyPage> {
  String kDevDailiKey = 'kDevDailiKey';

  String get hasProxy {
    return JDCache.getString(kDevDailiKey) ?? 'false';
  }

  TextEditingController controller = TextEditingController();

  @override
  void initState() {
    if (hasProxy.isNotEmpty) {
      controller.text = hasProxy;
    }
    super.initState();
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('设置Charles网络代理'),
        actions: [
          TextButton(
              onPressed: () {
                // TDChangeEnvironmentDialog().showEnvironmentDialog(context);
              },
              child: const Text('环境设置'))
        ],
      ),
      body: JDScrollView(
        needDismissKeyBord: true,
        children: [
          50.vGap,
          Text(
            hasProxy.isNotEmpty ? '    当前设置--有--代理$hasProxy' : '      没有设置网络代理',
            style: TextStyle(
                color: hasProxy.isNotEmpty ? Colors.red : Colors.black),
          ),
          Padding(
            padding: const EdgeInsets.all(30),
            child: JDTextField(
              '输入Charles代理ip',
              textEditController: controller,
              editingCompleteCallBack: (text) {},
            ),
          ),
          20.vGap,
          JDButton(
            text: '代理抓包模式进入首页',
            backgroundColor: Colors.red,
            onPressed: () {
              if (controller.text.isNotEmpty) {
                JDCache.setString(kDevDailiKey, controller.text);
              }
              SetProxyRequest().setProxy(hasProxy, "8888");
              Future.delayed(const Duration(milliseconds: 300), () {
                jump();
              });
            },
          ),
          30.vGap,
          JDButton(
            text: '非代理模式 进入首页',
            onPressed: () {
              jump();
            },
          ),
        ],
      ),
    );
  }

  jump() {
    RouterUtil.popUntil(context);
    RouterContractPage.instance.splashPage
        .push(context, hideAnimation: true, replacement: true);
  }
}

// dio网络代理抓包设置
class SetProxyRequest {
  void setProxy(String? proxyIP, String? proxyPort) {
    String? localProxy = "PROXY $proxyIP:$proxyPort";
    jdLog("localProxy=$localProxy");
    (TDDioInstance().dio.httpClientAdapter as IOHttpClientAdapter)
        .onHttpClientCreate = (client) {
      client.badCertificateCallback = (cert, host, port) {
        return true;
      };
      client.findProxy = (uri) {
        //  "PROXY 192.168.1.1:8888";
        return localProxy;
      };
      return null;
    };
  }
}
